let listLoad = require('../../common/mixinList');
Page({
    behaviors: [listLoad],
    data: {
        reqUrl: '/api/user/debt',
        payLoading: false,
        usePrivateMethod: true //使用私有方法
    },
    onLoad: function(options) {
        this.getList(true)
    },
    // getParamsData() { //页面特殊的请求参数，需要时在页面内覆盖此方法，默认返回{}
    //     return { so: this.data.searchText }
    // },
    privateMethod(refresh, res) {
        return new Promise(resolve => {
            res.data.forEach(item => {
                if (Number(item.debt_amount) > 0) {
                    item.needPay = true
                    item.status = '待支付欠款'
                } else if (Number(item.debt_amount) < 0) {
                    item.status = '待平台打款'
                } else {
                    item.status = '无欠款'
                }
            })
            resolve(res.data)
        })
    },
    pay(e) {
        if (this.data.payLoading) return
        this.data.payLoading = true
        const { index } = e.currentTarget.dataset
        this.request({
            url: '/api/payment/debt',
            method: 'POST',
            loadingTip: '处理中',
            data: { debt_id: this.data.list[index].debt_id }
        }, ({ data }) => {
            wx.requestPayment({
                timeStamp: data.pay_data.timeStamp,
                nonceStr: data.pay_data.nonceStr,
                package: data.pay_data.package,
                signType: data.pay_data.signType,
                paySign: data.pay_data.paySign,
                success: () => {
                    wx.showToast({
                        title: '支付成功',
                        icon: 'success'
                    })
                    this.data.payLoading = false
                    
                    // this.setData({
                    //     ['list[' + index + '].needPay']: false,
                    //     ['list[' + index + '].debt_amount']: '0.00'
                    // })
                    this.getList(true)
                },
                fail: () => {
                    wx.showToast({
                        title: '支付失败',
                        icon: 'error'
                    })
                    this.data.payLoading = false
                }
            })
        }, () => {
            wx.showToast({
                title: '支付失败',
                icon: 'error'
            })
            this.data.payLoading = false
        })
    }
})